/*
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "It_posix_container.h"
#include <sched.h>
#include <unistd.h>
#include <sys/stat.h>
#include <limits.h>
#include <string>
#include <iostream>
#include <regex>

#define _GNU_SOURCE
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#define STACK_SIZE     (1024 * 1024)

static int childFun(void *p)
{
    pid_t pid = getpid();
    sleep(2);
    printf("child process:exit pid=%d\n", pid);
    return 3;
}

static UINT32 Testcase(VOID)
{
    pid_t parentPid;
    int childPid;
    void *pstk;
    int fd;
    int ret;
    int status;
    int setFlag;
    char targetpath[100];
    char  old_mnt_link[100];
    char  new_mnt_link[100];
    char  *containerType = "mnt";

    std::cout << "ItPosixContainerMnt008.setns start" << std::endl;

    parentPid = getpid();
    std::cout << "parentPid=" << parentPid << std::endl;
    pstk = malloc(STACK_SIZE);
    if (NULL == pstk) {
        printf("parent cannot malloc stack\n");
        return -1;
    }

    childPid = clone(childFun, (char *)pstk + STACK_SIZE, CLONE_NEWNS, NULL);
    if (childPid == -1)
    {
        printf("parent: clone failed, child pid[%d]\n", childPid);
        ICUNIT_ASSERT_NOT_EQUAL(childPid, -1, childPid);
        return -1;
    }

    auto linkBuffer = readlink_container(parentPid, containerType);
    printf("parent: get caller mnt vnum old success\n");
    sprintf(old_mnt_link, "%s", linkBuffer.c_str());
    sprintf(targetpath, "/proc/%d/container/mnt", childPid);
    fd = open(targetpath, O_RDONLY | O_CLOEXEC);
    printf("parent: fd open target(childPid) at path=%s, fd=%d, errno=%d\n", targetpath, fd, errno);
    if (fd < 0)
    {
        printf("parent:get target(childPid) mnt fd failed, fd=%d\n", fd);
        return -1;
    }

    printf("parent: setns start, flag=%#x\n", setFlag);
    setFlag = CLONE_NEWNS;
    ret = setns(fd, setFlag);
    printf("parent:parentPid=%d setns(%d,%#x) result is %d\n", parentPid, fd, setFlag, ret);
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);

    ret = close(fd);
    printf("parent: close (%d) result is %d\n", fd, ret);
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);

    printf("parent: get caller mnt vnum parentPid=%d\n", parentPid);
    linkBuffer = readlink_container(parentPid, containerType);
    printf("parent: get caller mnt vnum new success\n");

    sprintf(new_mnt_link, "%s", linkBuffer.c_str());
    int strsize = strlen(new_mnt_link);

    printf("expect NOT equal mnt link vnum (old,new),actual:(%s,%s)\n", old_mnt_link, new_mnt_link);
    ICUNIT_ASSERT_SIZE_STRING_NOT_EQUAL(old_mnt_link, new_mnt_link, strsize, errno);

    ret = waitpid(childPid, &status, 0);
    printf("waitpid return value of waitpid is %d\n", ret);
    ICUNIT_ASSERT_EQUAL(ret, childPid, ret);

    int exitCode = WEXITSTATUS(status);
    printf("exit code of child process is %d\n", exitCode);
    ICUNIT_ASSERT_EQUAL(exitCode, 3, exitCode);
    return 0;
}

VOID ItPosixContainerMnt008(void)
{
    TEST_ADD_CASE("IT_POSIX_CONTAINER_MNT_008", Testcase, TEST_POSIX, TEST_CONTAINER, TEST_LEVEL0, TEST_FUNCTION);
}
